<?php

namespace app\models;

use Yii;
use yii\base\Exception;
use yii\data\Pagination;
use yii\db\ActiveRecord;


class Storage extends ActiveRecord
{


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['pur_name','number','isp_id','package','simtype','price','total_price'], 'required'],
            ['pur_time' ,  'filter', 'filter' => function(){ return strtotime($this->pur_time); }],
            ['pur_name', 'string'],
            ['silent_over' ,  'filter', 'filter' => function(){ return strtotime($this->silent_over); }],
            ['number','number'],
            ['order','required'],
            ['price','number'],
            ['total_price','number'],
            ['isp_id', 'exist', 'targetClass' => Isp::class, 'targetAttribute' => 'id'],
            ['package_id', 'exist', 'targetClass' => Package::class, 'targetAttribute' => 'id'],
            ['simtype', 'exist', 'targetClass' => Cardtype::class, 'targetAttribute' => 'id'],
            ['silent_time','number'],
            ['payment_type', 'required', 'message' => '{attribute}不能为空'],
            ['pre_time','number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pur_name'=> '采购项目名称',
            'isp_id'     => '运营商',
            'package_id' => '套餐',
            'number'  => '采购数量',
            'simtype' => '卡类型',
            'price'   => '单价',
            'total_price' => '总价',
            'order'   => '订单号',
            'payment_type'=>'付费方式',
            'silent_time' => '沉默期时长',
            'pre_time'=> '单卡预存时长',

        ];
    }

    /**
     * 生成新的批次号
     * @param integer $mid 管理用户编号，0表示系统自动添加
     * @return string
     */
    public static function generateBatchNo($mid = 0)
    {
        return date('YmdHis') . Util::randomStr(6) . $mid;
    }
    
    public function getApi()
    {
        return $this->hasOne(Api::class, ['id' => 'api_id']);
    }

    /**
     * 关联运营商
     * @return \yii\db\ActiveQuery
     */
    public function getIsp()
    {
        return $this->hasOne(Isp::class, ['id' => 'isp_id']);
    }

    /**
     * 关联套餐
     * @return \yii\db\ActiveQuery
     */
    public function getPackage()
    {
        return $this->hasOne(Package::class, ['id' => 'package_id']);
    }
    public function getPackageIsp(){
        return $this->hasOne(PackageIsp::class, ['id' => 'package_id']);
    }
    /**
     * 关联卡类型
     * @return \yii\db\ActiveQuery
     */
    public function getType(){
        return $this->hasOne(Cardtype::class,['id' => 'simtype']);
    }


}

